package com.ruoyi.websocker;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.websocker.mapper.TChatMessageMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "broker-a",consumerGroup = "chat_producer")
public class RocketMQConsumerListener implements RocketMQListener<String> {
    @Autowired
    private TChatMessageMapper tChatMessageMapper;
    @Override
    public void onMessage(String message) {
        MessageBean messageBean= JSON.parseObject(message,MessageBean.class);
        String status="未读";
        int re=0;
        if (messageBean.getRole().equals("客服")){
            re=tChatMessageMapper.insertMessgae(messageBean.getTargetId(),messageBean.getMsg(),status,messageBean.getId(),messageBean.getRole());
        }   else {
            re=tChatMessageMapper.insertMessgae(messageBean.getId(),messageBean.getMsg(),status,messageBean.getTargetId(),messageBean.getRole());
        }
        if (re==0){
            System.out.println("失败");
        }else {
            System.out.println("成功");
        }
    }
}
